<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.app
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       cake
 * @subpackage    cake.app
 */
class AppController extends Controller {
	var $uses=array('User');
	var $helpers = array('Js','Session');
	var $components = array('Session','Auth','Cookie');
	
   function beforeFilter(){
			Security::setHash("md5");
        	$this->Auth->authenticate = ClassRegistry::init('User');
            $user = $this->Auth->user();
            
            if(empty($user) && $this->params['action'] != 'login'){
			$this->redirect(array('controller' => 'users', 'action' => 'login'));
		      }
       	$this->Auth->loginAction 	= array('controller' => 'users', 'action' => 'login');
		$this->Auth->loginRedirect 	= array('controller'=>'pages', 'action' => 'home');
		$this->Auth->loginError 	= 'Tên tài khoản hoặc mật khẩu sai';
		$this->Auth->authError 		= 'Bạn cần phải đăng nhập hệ thống để sử dụng các chức năng';
		$this->Auth->logoutRedirect = '/';    
   }
    
  
  function beforeRender() {
        // Tạo biến curUser trong session
        $curUser=$this->Auth->user();
		$group_id = $curUser['User']['group_id'];
		$this->Session->write('cur_group',$group_id);
		//echo $group_id;
		//debug($curUser);
        if(count($curUser)>0){
			switch ($group_id){
			case 1 :
				$hoten = $this->User->query("SELECT docgias.*
                        from docgias, users
                            where users.username = docgias.maso
								and users.id=".$curUser['User']['id']);
								
				//echo $curUser['User']['id']; 
				$quyen= $this->User->query("SELECT phanquyens.role_id
                                        from phanquyens, docgias, users
                                       where phanquyens.group_id = users.group_id and users.id=".$curUser['User']['id']);
            
				$ten = $hoten[0]['docgias']['tennv'];
				$this->Session->write('cur_ten',$ten);
				$ho_ten=$this->Session->read('cur_ten');
				
				$id = $hoten[0]['docgias']['id'];
				$this->Session->write('cur_dg',$id);
				$cur_dg=$this->Session->read('cur_dg');
				
				$curid = $curUser['User']['id'];
				$this->Session->write('cur_id',$curid);
				$cur_id=$this->Session->read('cur_id');
				
				$this->Session->write('cur_role',$quyen);
				$role_id=$this->Session->read('cur_role');
				break;
			 case 2 :
				$hoten = $this->User->query("SELECT nhanviens.*
                        from nhanviens, users
                            where group_id = '2'
								and users.id=".$curUser['User']['id']);
                 
				$quyen= $this->User->query("SELECT phanquyens.role_id
                                        from phanquyens, docgias, users
                                       where phanquyens.group_id = users.group_id and users.id=".$curUser['User']['id']);
				
                $ten = $hoten[0]['nhanviens']['tennv'];
				$this->Session->write('cur_ten',$ten);
				$ho_ten=$this->Session->read('cur_ten');
                
                $id = $hoten[0]['nhanviens']['id'];
                $this->Session->write('cur_nvid',$id);
                $cur_nvid=$this->Session->read('cur_nvid');
				
				$curid = $curUser['User']['id'];
				$this->Session->write('cur_id',$curid);
				$cur_id=$this->Session->read('cur_id');
				
				$this->Session->write('cur_role',$quyen);
				$role_id=$this->Session->read('cur_role');
				break;
			case 3 :
				$hoten = $this->User->query("SELECT nhanviens.*
                        from nhanviens, users
                            where nhanviens.masonv=users.username
								and users.id=".$curUser['User']['id']);
				$quyen= $this->User->query("SELECT phanquyens.role_id
                                        from phanquyens, docgias, users
                                       where phanquyens.group_id = users.group_id and users.id=".$curUser['User']['id']);
            
				$ten = $hoten[0]['nhanviens']['tennv'];
				$id = $hoten[0]['nhanviens']['id'];
				$this->Session->write('cur_ten',$ten);
				$this->Session->write('cur_nvid',$id);
				$ho_ten=$this->Session->read('cur_ten');
				
				$curid = $curUser['User']['id'];
				$this->Session->write('cur_id',$curid);
				$cur_id=$this->Session->read('cur_id');
				
				$this->Session->write('cur_role',$quyen);
				$role_id=$this->Session->read('cur_role');
				break;
			} 
        }
     
        $this->set(compact('curUser','hoten','ho_ten','cur_id','quyen','role_id','cur_dg','cur_nvid','group_id'));
        
	}
/**
 *  Giao dich muon tra
 *  @var Giaodichmuontra
 */
var $Giaodichmuontra;

/**
 *  Hinh thuc xu phat
 *  @var Hinhthucxuphat
 */
var $Hinhthucxuphat;

/**
 *  Bien lai phat
 *  @var Bienlaiphat
 */
var $Bienlaiphat;

/**
 * Chi tiet vi pham
 *  @var Chitietvipham
 */
var $Chitietvipham;

/**
 *  Loai doc gia
 *  @var Loaidocgia
 */
var $Loaidocgia;

/**
 *  Doc gia
 *  @var Docgia
 */
var $Docgia;

/**
 *  NXB
 *  @var Nhaxuatban
 */
var $Nhaxuatban;

/**
 *  Linh vuc
 *  @var Linhvuc
 */
var $Linhvuc;


/**
 *  Dausach
 *  @var Dausach
 */
var $Dausach;

/**
 *  Thuoc lop
 *  @var Thuoclop
 */
var $Thuoclop;

/**
 *  Lop
 *  @var Lop
 */
var $Lop;

/**
 *  Nganh
 *  @var Nganh
 */
var $Nganh;

/**
 *  Khoa hoc
 *  @var Khoahoc
 */
var $Khoahoc;

/**
 *  Nhan vien
 *  @var Nhanvien
 */
var $Nhanvien;

/**
 *  Hoa don nhap
 *  @var Hoadonnhap
 */
var $Hoadonnhap;

/**
 *  Chi tiêt hd nhap
 *  @var Chitietnhap
 */
var $Chitietnhap;

/**
 *  Ngon ngu
 *  @var Ngonnguk
 */
var $Ngonnguk;

/**
 *  Cuon sach
 *  @var Cuonsach
 */
var $Cuonsach;

/**
 *  
 *  @var RequestHandlerComponent
 */
var $RequestHandler;

/**
 *  
 *  @var SessionComponent
 */
var $Session;
/**
 *  
 *  @var CookieComponent
 */
var $Cookie;
/**
 *  
 *  @var EmailComponent
 */
var $Email;

}
